package com.heima.admin.controller.v1;

import com.heima.admin.service.ChannelService;
import com.heima.model.admin.dtos.ChannelDTO;
import com.heima.model.admin.pojos.AdChannel;
import com.heima.model.common.dtos.ResponseResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author mrchen
 * @date 2022/7/1 11:30
 */

@RestController // @ResponseBody  + @Controller
@RequestMapping("/api/v1/channel")
@Api(value = "频道管理controller",tags = "频道管理controller",description = "频道管理controller")
public class ChannelController {
    @Autowired
    private ChannelService channelService;

    //频道列表数据查询(nacos注册中心需配置)
    @ApiOperation("查询全部频道")
    @GetMapping("/channels")
    public ResponseResult findAll() {
        List<AdChannel> list = channelService.list();
        return ResponseResult.okResult(list);
    }


    @ApiOperation("条件分页查询频道列表")
    @PostMapping("/list")
    // @RequestParam  ?name=xxx&age=18   xxx=xxxx         @PathVariable  @RequestBody
    public ResponseResult list(@RequestBody ChannelDTO dto){
        return channelService.findListByQuery(dto);
    }
    @ApiOperation(value = "保存频道信息",notes = "保存时 会校验： 频道名称不为空  频道名称不能超过10个字符，名称不能重复")
    @PostMapping("/save")
    public ResponseResult insert(@RequestBody AdChannel channel){
        return channelService.insert(channel);
    }
    @ApiOperation(value = "修改频道信息",notes = "修改 会校验： 如果频道名称有改动 会检查频道名称不为空  频道名称不能超过10个字符，名称不能重复")
    @PostMapping("/update")
    public ResponseResult update(@RequestBody AdChannel channel){
        return channelService.update(channel);
    }
    @ApiOperation(value = "根据id删除频道",notes = "如果 频道有效，不能删除")
    @GetMapping("/del/{id}")
    public ResponseResult delete(@PathVariable Integer id){
        return channelService.delete(id);
    }
}
